package edu.uwosh.cs342.project3;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class QuestionList extends ListActivity {

	//  Request code for getting data back
	private static final int REQUEST_CODE = 10;
	
	private static final int MC = 0;
	private static final int FB = 1;
	private static final int TF = 2;
	private static final int FINISH = 3;
	
	int totalPoints = 0;
	boolean questionAnswered[];
	
	String quizName;
	int numberOfQuestions = 0;
	int questionType[];
	String[] questionStatments;
	String[] questionAnswers;
	String[][] questionOptions;
	
	//******* change when parsing is complete

	@Override
	protected void onCreate(Bundle savedInstanceState){
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Bundle extras = getIntent().getExtras();
		quizName = extras.getString("quizSelected");
		
		//******  sql query will get the quiz named defined in quizName
		Toast.makeText(this, "Taking quiz, " + quizName , Toast.LENGTH_LONG).show();
		
		
		//  get xml quiz and put into file
		//String url = "";
		//URI connection = null;
		//connection = new URI(url);
		//File xmlFile = new File(connection);
		
		//Parse xmlParser = null;

		//xmlParser = new Parse(xmlFile);
		
		//  Initialize quiz arrays
		//initialize(xmlParser);
		
		//  @@@@@@@@@@@@  Take this out when we can get the file @@@@@@@@@@@@@@
		runDummyData();

		//  Labels for questions in list
        String[] questionNumber = new String[(numberOfQuestions + 1)]; 
        for(int i = 0; i < numberOfQuestions; i++)
        {
        	questionNumber[i] = "Question #" + (i + 1);
        }
        questionNumber[numberOfQuestions] = "Finish";
        
        //  Make list
        setListAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, questionNumber));
	}
	
	public void initialize(Parse xmlParser){
		questionAnswered = new boolean[(numberOfQuestions+1)];
		//  Initialize questionAnswered array
		for (int i = 0; i < numberOfQuestions; i++)
			questionAnswered[i] = false;
		questionAnswered[numberOfQuestions] = false;
		//  Other arrays initialized
		numberOfQuestions = xmlParser.getNumberOfQuestions();
		questionStatments = xmlParser.getQuestionStatements();
		questionAnswers = xmlParser.getQuestionAnswers();
		questionOptions = xmlParser.getQuestionOptions();
		questionType = xmlParser.getQuestionTypeArray();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		
		Intent questionIntent;
		
		if ( (questionAnswered[position] == false))
		{
			//  Intent for multiple choice question
			if (questionType[position] == MC) { 
				questionIntent = new Intent("android.intent.action.MULTIPLECHOICE");
				questionIntent.putExtra("question", "this is the multiple choice question"); //  questionStatement[position]
				questionIntent.putExtra("choice1", "this is choice1"); //  questionOption[position][0]
				questionIntent.putExtra("choice2", "this is choice2"); //  questionOption[position][1]
				questionIntent.putExtra("choice3", "this is choice3"); //  questionOption[position][2]
				questionIntent.putExtra("correctAnswer", "this is the correct answer"); //  questionAnswer[position]
				questionIntent.putExtra("submitArray", questionAnswered);
				questionIntent.putExtra("position", position);
				//  Move to quiz question activity
				startActivityForResult(questionIntent, REQUEST_CODE);
			}
			//  Intent for short answer question
			else if(questionType[position] == FB){
				questionIntent = new Intent("android.intent.action.FILLBLANK");
				questionIntent.putExtra("question", "this is the question"); //  questionStatement[position]
				questionIntent.putExtra("correctAnswer", "answer"); //  questionAnswer[position]
				questionIntent.putExtra("submitArray", questionAnswered);
				questionIntent.putExtra("position", position);
				//  Move to quiz question activity
				startActivityForResult(questionIntent, REQUEST_CODE);
			}
			//  Intent for true/false question
			else if(questionType[position] == TF){
				questionIntent = new Intent("android.intent.action.TRUEFALSE"); 
				questionIntent.putExtra("question", "this is the t/f question"); //  questionStatement[position]
				questionIntent.putExtra("correctAnswer", "True"); //  questionAnswer[position]
				questionIntent.putExtra("submitArray", questionAnswered);
				questionIntent.putExtra("position", position);
				//  Move to quiz question activity
				startActivityForResult(questionIntent, REQUEST_CODE);
			} else if(questionType[position] == FINISH) {
				if (done()){
					//startActivity(new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
					
					
					Intent finalIntent = new Intent("android.intent.action.FINALSCORE");
					finalIntent.putExtra("score", totalPoints);
					finalIntent.putExtra("numberOfQuestions", numberOfQuestions);
					finalIntent.putExtra("title", quizName);
					Toast.makeText(this, "You are done", Toast.LENGTH_SHORT).show();
					startActivity(finalIntent);
					
					//  startActivity(new Intent(this, MainActivity.class)
					// .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
				}	
				else{
					//  Show questions not completed
					String unanswered = "";
					for(int i = 0; i < numberOfQuestions; i++)
						if (questionAnswered[i] == false)
							unanswered = unanswered + (i+1) + ", ";
					
					Toast.makeText(this, "Answer the rest of the questions: " + unanswered, Toast.LENGTH_SHORT).show();
				}
			}
			else{
				//  ****put code that makes sure user has answered all questions
				finish();
			}
		}else{
			//  If user trys to answer a question twice...
			Toast.makeText(this, "Question #" + (position + 1) + " has already been answered",
					Toast.LENGTH_SHORT).show();
		}
	}
	
	public boolean done(){
		//  Check if all the questions have been attempted
		for(int i = 0; i < numberOfQuestions; i++){
			if (questionAnswered[i] == false)
				return false;
		}
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		//  Get point back from activity if answered correctly
		if (data.hasExtra("point") && requestCode == REQUEST_CODE) {
			Toast.makeText(this, "You received " + data.getExtras().getInt("point") + 
					" point",	Toast.LENGTH_SHORT).show();
		}
		
		totalPoints = totalPoints + data.getExtras().getInt("point");
		questionAnswered = data.getExtras().getBooleanArray("returnSubmit");
		
		Toast.makeText(this, "for a total of " + totalPoints + " / " + numberOfQuestions,
				Toast.LENGTH_SHORT).show();
		
	}
	
	public void runDummyData(){
		// **** change later
				numberOfQuestions = 3;
				questionAnswered = new boolean[(numberOfQuestions+1)];
				
				//  Initialize questionAnswered array
				for (int i = 0; i < numberOfQuestions; i++)
					questionAnswered[i] = false;
				questionAnswered[numberOfQuestions] = false;
				
				//  Initialize questionType array
				questionType = new int[(numberOfQuestions+1)];
				//  ******  use xml to find question types
				for(int i = 0; i < numberOfQuestions; i++)
					questionType[i] = FB;
				questionType[0] = TF;
				questionType[1] = MC;
				
				questionType[numberOfQuestions] = FINISH;
	}
	
}
